/**
 ********************************************************************
 * @author roocket
 * @file    sound_wav_pcm.c
 * @version V0.0.0
 * @date    2024/4/27
 * @brief   This file is to indicate wav pcm functions. 
 * @attention Code file rules:
 * rule: file encoding use UTF8;
 * rule: max line length 120 characters;
 * rule: line separator \r\n;
 * rule: use clion auto code format tool.
 */
#include "sound_wav_pcm.h"

int sound_wav_to_pcm_format_file(const char *wav_file, const char *pcm_file)

{
    FILE *wav = NULL;
    FILE *pcm = NULL;
    int res;
    char wav_buffer[1024] = "";

    wav = fopen(wav_file, "rb");
    if (NULL == wav) {
        perror("fopen wav file error");
        return -1;
    }

    pcm = fopen(pcm_file, "wb");
    if (NULL == pcm) {
        perror("fopen pcm file error");
        goto wav_file_error;
    }

    /* make an offset for wav file pointer */
    fseek(wav, sizeof(struct sound_wav_format), SEEK_SET);

    /* move file content */
    while (1) {
        memset(wav_buffer, 0x00, 1024);

        if (feof(wav)) {
            printf("touch the end of file %s\n", wav_file);
            break;
        }
        res = fread(wav_buffer, sizeof(char), sizeof(wav_buffer), wav);

        res = fwrite(wav_buffer, sizeof(char), res, pcm);
    }

    fclose(wav);
    fclose(pcm);
    return 0;

wav_file_error:
    fclose(wav);
    return -1;
}


